package com.blog.controller;
import com.blog.Utils.Result;
import com.blog.annotion.LoginAnnotatio;
import com.blog.annotion.ViewCountAnnotation;
import com.blog.base.ThreadLocalUserId;
import com.blog.bean.domain.DiscussPost;
import com.blog.bean.dto.PostDTO;
import com.blog.service.DiscussPostService;
import com.blog.bean.vo.PostAndCommentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;

/**
 * @author 任浩
 * @version 1.0
 */

@RestController
@Slf4j
@RequestMapping("/post")
public class DiscussPostController {

    @Autowired
    private DiscussPostService discussPostService;

    @PostMapping("/add")
    @LoginAnnotatio
    public Result add(@RequestBody DiscussPost discussPost, HttpSession session){
        log.info("发布帖子的方法");
        System.out.println(discussPost);
        Integer userId = (Integer)session.getAttribute("userId");
        if(userId == null){
            userId = Integer.parseInt(ThreadLocalUserId.get());
        }
        discussPostService.add(discussPost,userId);
        log.info("发布帖子成功");

        return Result.success();
    }

    //拦截当前方法，如果没有登录是不能访问帖子的详细信息的。
    @LoginAnnotatio
    @ViewCountAnnotation
    @GetMapping
    public Result queryPostByUserId(PostDTO postDTO,HttpSession session){
        //得到当前的用户id
        Integer userId = (Integer)session.getAttribute("userId");
        if(userId == null){
            userId = Integer.parseInt(ThreadLocalUserId.get());
        }
        PostAndCommentVO postAndCommentVO = discussPostService.queryPostByUserId(postDTO,userId);

        return Result.success(postAndCommentVO);
    }

    /**
     * 对帖子进行置顶的操作
     * @param postId
     * @return
     */
    @PutMapping("/top")
    @LoginAnnotatio
    public Result setTop(@RequestParam("id") Integer postId){
        discussPostService.setTop(postId);

        return Result.success();
    }

    /**
     * 对帖子进行加精的操作
     * @param postId
     * @return
     */
    @PutMapping("/wonderful")
    @LoginAnnotatio
    public Result setWonderful(@RequestParam("id") Integer postId){
        discussPostService.setWonderful(postId);

        return Result.success();
    }

    /**
     * 对帖子进行删除操作
     * @param postId
     * @return
     */
    @GetMapping("/delete")
    @LoginAnnotatio
    public Result delete (@RequestParam("id") Integer postId){
        discussPostService.delete(postId);
        return Result.success();
    }


}
